package net.lzzy.practices.services;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.CourseRepository;
import net.lzzy.practices.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author  Jaymo on 2022/5/19
 * Description:
 * implements CourseServie  1.实现接口 2.servive  3.增加 repois字段      4.构造器注入
 */
@Service
public class CourseServiceImpl implements CourseService  {

    private  final  CourseRepository repository;

    @Autowired
    public  CourseServiceImpl(CourseRepository repository){
        this.repository = repository;
    }

    @Override
    public List<Course> get(){
        return  repository.findAllByOrderByUpdateTimeDesc();
    }

    @Override
    public  List<Course> get(boolean open){
        return  repository.findCoursesByOpenEqualsOrderByUpdateTimeDesc(open);
    }

    @Override
    public  List<Course> get(int page, int size)  {
        return  repository.findAllByOrderByUpdateTimeDesc(PageRequest.of(page, size));
    }

    @Override
    public  List<Course> get(boolean open, int page, int size){
        return  repository.findCoursesByOpenEqualsOrderByUpdateTimeDesc(PageRequest.of(page, size), open);
    }

    @Override
    public Integer count(){
        return  repository.countAllBy();
    }

   @Override
   public  Integer count(boolean open){
        return  repository.countCourseByOpenEquals(open);
   }

    @Override
    public  Course getById(UUID id){
        return  repository.findById(id).orElse(null);
    }

    @Override
    public List<Course> getTeachersCourses(UUID teacherId){
        return repository.findCoursesByTeacherIdEqualsOrderByCreateTimeDesc(teacherId);
    }


    @Override
    public  List<Course> getStudentsCourses(UUID studentId){
        User student = new User();
        student.setId(studentId);
        return repository.findCoursesByStudentsContainsAndOpenOrderByUpdateTimeDesc(student,true);
    }

    @Override
    public Course add(Course course){
        return  repository.save(course);
    }


    @Override
    public Course update(Course course, String token){
        UUID teacherID = course.getTeacher().getId();
        if (AuthUtils.invalid(teacherID, token)){
            return  null;
        }
        course.setUpdateTime(new Date());
        return  repository.save(course);
    }

    @Override
    public Course openCloseCourse(UUID id, boolean open, String token){
        Course course = repository.findById(id).orElse(null);
        if (course != null ){
            course.setOpen(open);
            return  update(course, token);
        }
        return  null;
    }

    @Override
    public Messages remove(UUID id, String token){
      Course course =  repository.findById(id).orElse(null);
      if (course == null){
          return  Messages.WRING_ID;
      }
      if (AuthUtils.invalid(course.getTeacher().getId(),token)){
          return  Messages.No_PERMISSION;
      }
      if (course.getChapters().size() > 0 || course.getStudents().size() > 0 || course.getApplicants().size() > 0){
          return Messages.RELATED_DATA;
      }
      repository.delete(course);
      return  Messages.SUCCESS;
    }
}
